package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"strconv"
	"user-login1/models/mysql"

	"user-login1/userrpc/internal/svc"
	"user-login1/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 加完善个人信息
func (l *UserPerfectLogic) UserPerfect(in *userrpc.UserPerfectRequest) (*userrpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	if err := user.FindUserShow(mysql.DB, strconv.FormatInt(in.UserId, 10)); err != nil {
		return nil, errors.New("用户不存在,查询失败")
	}
	user = mysql.User{
		Model:      gorm.Model{ID: uint(in.UserId)},
		HeadImage:  in.HeadImage,
		NickName:   in.NickName,
		Address:    in.Address,
		WechatCode: in.WechatCode,
	}
	if err := user.UserPerfect(mysql.DB); err != nil {
		return nil, errors.New("完善信息失败")
	}
	return &userrpc.UserPerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
